
/////////////////////// GLOBAL VARIABLE ////////////////////////


//--------------------- END GLOBAL VARIABLE ------------
///////////////// TURN ON/OFF AJAX LOADER //////////////////////
function gReportShowLoader(show){
	if(show==true){	
		$('#loading').show();
		$('div#actionButtons').hide();
	}
	else{
		$('#loading').hide();
		$('div#actionButtons').show();
	}
}

function disableGlobalButtons(flag){
	if(flag){
		$('input#createGroup').attr('disabled','disabled');
		$('input#createReport').attr('disabled','disabled');
		$('input#viewAllReports').attr('disabled','disabled');
	}
	else{
		$('input#createGroup').removeAttr('disabled');
		$('input#createReport').removeAttr('disabled');
		$('input#viewAllReports').removeAttr('disabled');
	}
}
//--------------------- END TURN ON/OFF AJAX LOADER --------------

//////////////////////// LOADING REPORT /////////////////////////
function loadReports(groupId) {
	$("#confirmDelete").remove();
	gReportShowLoader(true);
	$.ajax ({		
		url:	  	"renderReports?set_ajax_view",
		data:		"groupId=" + groupId,		
		dataType: 	"html",
		beforeSend: function() {	
		},
		complete: 	function() {
		
		},
		success:  function(data) {
			$("#viewAllReportsContent").html(data);
			gReportShowLoader(false);			
		}
	});
}

//--------------------- END LOADING REPORT------------------------

//////////////////////// VIEW ALL REPORTS ////////////////////////
function viewAllReports_org(){
	disableGlobalButtons(true);
	$("div#gReportContent").html('');
	$.ajax ({		
		url:	  	"viewAllReports?set_ajax_view",		
		dataType: 	"html",
		beforeSend: function() {	
		},
		complete: 	function() {
			$('div#gReportContent').show("fast");			
		},
		success:  function(data) {
			$("div#gReportContent").html(data);		
			disableGlobalButtons(false);	
		}
	});
}

function viewAllReports(){   
    url = "../GeneralReport/viewAllReports";
    window.location=url;    
    
}

//--------------------- END VIEW ALL REPORTS-----------------------

//////////////////////// DELETE|CREATE|EDIT GROUP /////////////////////////////
function deleteGroup(groupId){
	$("#confirmDelete input[id='groupId']").val(groupId);
	$('#confirmDelete').dialog('open');	
	$("#deleteReports").removeAttr('checked');
	$("#notDeleteReports").attr('checked', 'checked');
}

function deleteReportGroup(){
	var deleteReports;
	$("input[name='deleteReports']").each(function(){
			if($(this).is(':checked')){
				deleteReports	=	$(this).val();
			}
	});
	$.ajax ({		
		url:	  	"deleteGroup?set_ajax_view",
		data:		"groupId=" + $('#groupId').val()+
					"&deleteReports=" + deleteReports,
		dataType: 	"JSON",
		beforeSend: function() {
			$('#confirmDelete').dialog('close');	
		},
		success:  function(data) {
			if(data.deleted=1){
				// Reload view all reports page
				$("#confirmDelete").remove();
				viewAllReports();
			}
		}
	});
}

function editGroup(groupId){
	disableGlobalButtons(true);
	$("div#gReportContent").html('');
	$.ajax ({
		type: 		"GET",		
		url:	  	"loadEditGroupPage?set_ajax_view",
		data:		"groupId=" + groupId,
		dataType: 	"html",
		beforeSend: function() {	
		},
		success:  function(data) {
			$('div#gReportContent').html(data);
			disableGlobalButtons(false);
		}
	});
}

function saveGroupInfo(){
	if(!isReadyToSubmitGroup()){
		return;
	}
	gReportShowLoader(true);
	var submitted	=	$('#frmEditGroup').formSerialize();
	$.ajax ({
		type: 		"POST",		
		url:	  	"saveGroupInfo?set_ajax_view",
		data:		submitted,
		dataType: 	"html",
		beforeSend: function() {	
		},
		success:  function(data) {			
			gReportShowLoader(false);		
			viewAllReports();
		}
	});
}

function isReadyToSubmitGroup(){
	if(!isNotEmpty($('input#groupName'), _("Tên nhóm trống"), "errMsgEditGroup")){
		return false;
	}
	return true;
}
//--------------------- END DELETE|CREATE|DELETE GROUP-----------------------

//////////////////////// EDIT REPORT //////////////////////////
function editReport(reportId){
	disableGlobalButtons(true);
	$("div#gReportContent").html('');
	$.ajax ({
		type: 		"GET",		
		url:	  	"loadEditReportPage?set_ajax_view",
		data:		"reportId=" + reportId,
		dataType: 	"html",
		beforeSend: function() {	
		},
		success:  function(data) {
			$('div#gReportContent').html(data);
			disableGlobalButtons(false);
		}
	});
}

function isReadyToSubmitReport(){
	if(!isNotEmpty($('input#reportName'), _("Tên báo cáo trống"), "errMsgEditReport")){
		return false;
	}
	return true;
}

function saveReportInfo(){
	if(!isReadyToSubmitReport()){
		return;
	}
	gReportShowLoader(true);
	var submitted	=	$('#frmEditReport').formSerialize();
	$.ajax ({
		type: 		"POST",		
		url:	  	"saveReportInfo?set_ajax_view",
		data:		submitted,
		dataType: 	"json",
		beforeSend: function() {	
		},
		success:  function(data) {			
			gReportShowLoader(false);
			if(data.saved=1){
				$('input#reportId').val(data.reportId);
				$('#msgEditReport').text(_("Lưu báo cáo thành công")).css('color', 'green').show().fadeOut(5000);
				$("#btnLoadInputDataPage").removeAttr('disabled');				
			}
			
		}
	});
}

function inputData(reportId){
	$.ajax ({
		type: 		"GET",		
		url:	  	"loadInputDataPage?set_ajax_view",
		data:		"reportId=" + reportId,
		dataType: 	"html",
		beforeSend: function() {
			$("div#gReportContent").html('');
		},
		success:  function(data) {
			$("div#gReportContent").html(data);
		}
	});
}

function loadInputDataPage(){
	var reportId	=	$('input#reportId').val();
	inputData(reportId);
}
function deleteRow(id){
	id	=	"row_" + id;
	$("#tblInputData tbody tr\[id=\'" + id + "\'\]").remove();
}

function addRow(){
	if(!checkNumber($('input#rowNumber'), _('Số hàng không hợp lệ'), 'errMsgInputData', '#90D9F1')){
		return;
	};
	var number		= 	$('input#rowNumber').val();	
	var lastId		=	$("#tblInputData tbody tr:last").attr('id');
	var maxIndex	=	parseInt(lastId.replace('row_',''));
	// Build html table rows	
	var htmlRows	=	"";
	for(i=0; i< number; i++){
		index	=	maxIndex + 1 + i;
		htmlRows	+=	"<tr id='row_" +  index + "'>"	+
				"<td class='first'><input type='text' name='orders[]' id='order_" + index + "' style='width: 50px;' maxlength='3' onfocus='generateOrder(this.id)'></td>" +
				"<td><input type='text' name='metric[]' id='metric_" + index + "' style='width: 120px;' maxlength='45'></td>" +
				"<td><input type='text' name='data[]' id='data_" + index + "' style='width: 120px;' maxlength='12'></td>" +
				"<td class='last action'><a href=\'javascript:deleteRow\(" + index + "\)\'><img src='images/remove_small.png' title='"+_('Xóa dòng') + "' alt='" + _('Xóa') + "'></a></td></tr>";
	}
	$("#tblInputData tbody").append(htmlRows);
}

function viewReport(reportId){
	$.ajax ({
		type: 		"GET",		
		url:	  	"viewReport?set_ajax_view",
		data:		"reportId=" + reportId,
		dataType: 	"html",
		beforeSend: function() {
			$("div#gReportContent").html('');
		},
		success:  function(data) {
			$("div#gReportContent").html(data);
		}
	});
}

function saveReportData(){
	if(!isReadyToSubmitReportData()){
		return;
	}
	gReportShowLoader(true);
	var submitted	=	$('#frmInputData').formSerialize();
	$.ajax ({
		type: 		"POST",		
		url:	  	"saveReportData?set_ajax_view",
		data:		submitted,
		dataType: 	"json",
		beforeSend: function() {	
		},
		success:  function(data) {			
			gReportShowLoader(false);
			window.location  =   data.url;
		}
	});
}

function isReadyToSubmitReportData(){
	var res	=	true;
	$('#tblInputData tbody tr').each(function(){
		var index	=	parseInt($(this).attr('id').replace('row_',''));
		// Invalid case if metric has value but data has no value, or vice versa
		var metricId	=	"metric_" + index;
		var dataId		=	"data_" + index;
		if(($("#" + metricId).val()!='' && $("#" + dataId).val()=='') || 
			($("#" + metricId).val()=='' && $("#" + dataId).val()!='')){
				$('span#errMsgInputData').text("Dữ liệu không hợp lệ").show();
				$(this).find('input').css('border-color','red');
				res	= false;
		}
		// Invalid case if a data value is not a number
		else if (!checkFloatingNumber($("#" + dataId), _("Giá trị không hợp lệ (phải là một số)"),
		              'errMsgInputData', '#90D9F1')){
                res = false;
        }
        else {
			$(this).find('input').css('border-color','#90D9F1');
		}
	});
	if(res){
		$('span#errMsgInputData').text('');
	}
	return res;
}

function generateOrder(id){
	var index	=	parseInt(id.replace('order_','')) + 1;
	$('input#' + id).val(index);
}

function deleteReport(reportId){
	$('#confirmDeleteReport input[id=deletedReportId]').val(reportId);
	
	$('#deletedReportName').html($('#' + reportId).html());
	$('#confirmDeleteReport').dialog('open');		
}

function deleteReportSubmit() {
	var reportId = $('#deletedReportId').val();
	$.ajax ({
		type: 		"GET",		
		url:	  	"deleteReport?set_ajax_view",
		data:		"reportId=" + reportId,
		dataType: 	"html",
		beforeSend: function() {
			$('#confirmDeleteReport').dialog('close');
		},
		success:  function(data) {			
			$("#confirmDeleteReport").remove();
			viewAllReports();
		}
	});
}


//--------------------- END EDIT REPORT-----------------------
function showTableView(){
    if($('#showTableView span').text()==_('Xem bảng dữ liệu')) {
        $('#showTableView span').text(_('Ẩn bảng dữ liệu'));
    } else if($('#showTableView span').text()==_('Ẩn bảng dữ liệu')){
        $('#showTableView span').text(_('Xem bảng dữ liệu'));
    }
    switchDiv('tableViewDiv', 'dummy');
}